package com.lzy.system.organization.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.exception.BusException;
import com.lzy.system.organization.dto.SysNavAddDTO;
import com.lzy.system.organization.dto.SysNavSortDTO;
import com.lzy.system.organization.dto.convert.SysNavConvert;
import com.lzy.system.organization.dto.convert.SysNavUpdateDTO;
import com.lzy.system.organization.entity.SysNav;
import com.lzy.system.organization.mapper.SysNavMapper;
import com.lzy.system.organization.service.ISysNavService;
import com.lzy.system.organization.vo.SysNavUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 导航菜单 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2023-12-05
 */
@Service
public class SysNavServiceImpl extends ServiceImpl<SysNavMapper, SysNav> implements ISysNavService {

    SysNavConvert sysNavConvert;
    SysNavMapper sysNavMapper;

    @Autowired
    public SysNavServiceImpl(SysNavConvert sysNavConvert, SysNavMapper sysNavMapper) {
        this.sysNavConvert = sysNavConvert;
        this.sysNavMapper = sysNavMapper;
    }


    @Override
    public List<SysNav> getList() throws BusException {
        QueryWrapper<SysNav> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("order_index");
        queryWrapper.orderByAsc("id");
        List<SysNav> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SysNavUserVo> getListUserNav(String userId) throws BusException {
        List<SysNav> userNav = sysNavMapper.getUserNav(userId);
        List<SysNavUserVo> userVoListEntity = sysNavConvert.toUserVoListEntity(userNav);
        for (SysNavUserVo sysNavUserVo : userVoListEntity) {
            if(StrUtil.isNotBlank(sysNavUserVo.getOpenType())){
                if(sysNavUserVo.getOpenType().equals("1")){
                    sysNavUserVo.setOpenType("_iframe");
                }else if(sysNavUserVo.getOpenType().equals("2")){
                    sysNavUserVo.setOpenType("_blank");
                }else if(sysNavUserVo.getOpenType().equals("3")){
                    sysNavUserVo.setOpenType("_window");
                }
            }
        }
        class Tree{
            public List<SysNavUserVo> getTree(List<SysNavUserVo> treedata){
                List<SysNavUserVo> list = new ArrayList<>();
                for(SysNavUserVo obj:treedata){
                    int parentid = obj.getPid();
                    if(parentid == 0){
                        SysNavUserVo treeObject = ObjectUtil.clone(obj);
                        List<SysNavUserVo> target = getTarget(treedata, treeObject.getId());
                        if(target.size() == 0){
                            treeObject.setType(1);
                        }else{
                            treeObject.setType(0);
                        }
                        treeObject.setChildren(target);
                        list.add(treeObject );
                    }
                }
                return list;
            }
            /**
             * 获得指定节点
             * @param treedata
             * @param id
             * @return
             */
            private List<SysNavUserVo> getTarget(List<SysNavUserVo> treedata,int id) {
                List<SysNavUserVo> list = new ArrayList<>();
                for (SysNavUserVo obj : treedata) {
                    int pid = obj.getPid();
                    if (pid == id){
                        SysNavUserVo treeObject =  ObjectUtil.clone(obj);
                        List<SysNavUserVo> target = getTarget(treedata, treeObject.getId());
                        if(target.size() == 0){
                            treeObject.setType(1);
                        }else{
                            treeObject.setType(0);
                        }
                        treeObject.setChildren(target);
                        list.add(treeObject);
                    }
                }
                return list;
            }
        }
        return new Tree().getTree(userVoListEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysNavAddDTO dto) throws BusException {
        boolean save = save(sysNavConvert.addDTOToEntity(dto));
        if(!save){
            throw new BusException("新增失败");
        }
        return save;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysNavUpdateDTO dto) throws BusException {
        boolean save = updateById(sysNavConvert.updateDTOToEntity(dto));
        if(!save){
            throw new BusException("修改失败");
        }
        return save;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delById(long id) throws BusException {
        QueryWrapper<SysNav> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusException("当前导航有下级导航不能删除");
        }
        boolean b = removeById(id);
        if(!b){
            throw new BusException("删除失败");
        }
        return b;
    }

    @Override
    public boolean delBatch(List<Integer> list) throws BusException {
        boolean b = removeBatchByIds(list);
        if(!b){
            throw new BusException("删除失败");
        }
        return b;
    }

    @Override
    public boolean saveOrder(List<SysNavSortDTO> list) throws BusException {
        List<SysNav> sysNavs = sysNavConvert.sortDTOToListEntity(list);
        boolean b = updateBatchById(sysNavs);
        if(!b){
            throw new BusException("导航排序失败");
        }
        return true;
    }
}
